////////////////////////////////////////////////////////////////////////////////
//
//  WPS On Web Flex 基础库
//  版权所有(C) 1998-2009 珠海金山办公软件公司
//  保留所有权利。
//
//  注意：本文件为WPS机密，请勿外传。
//
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//
//	创建者	：卓洪涛
//	创建时间	：2008年09月22日21时09分24秒
//	功能描述	：仿Windows窗口
//
////////////////////////////////////////////////////////////////////////////////

package com.wps.shell.controls
{
	import com.wps.shell.controls.commonWindow.ComWindowBackGround;
	import com.wps.shell.events.WindowEvent;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.containers.Panel;
	import mx.controls.Button;
	import mx.core.ContainerCreationPolicy;
	import mx.core.ContainerLayout;
	import mx.core.mx_internal;
	import mx.events.CloseEvent;
	import mx.events.MoveEvent;
	import mx.events.ResizeEvent;
	import mx.managers.CursorManager;
	import mx.managers.CursorManagerPriority;
	
	import spark.components.Button;
	import spark.components.Group;
	import spark.components.SkinnableContainer;
	import spark.skins.spark.SkinnableContainerSkin;

	use namespace mx_internal;

	/**
	 * 窗口被激活事件。
	 * @eventType com.wps.shell.events.WindowEvent.WINDOW_ACTIVATE
	 */
	[Event(name="windowactivate", type="com.wps.shell.events.WindowEvent")]

	/**
	 * 窗口最大化事件。
	 * @eventType com.wps.shell.events.WindowEvent.WINDOW_MAXIMIZE
	 */
	[Event(name="windowmaximize", type="com.wps.shell.events.WindowEvent")]

	/**
	 * 窗口最小化事件。
	 * @eventType com.wps.shell.events.WindowEvent.WINDOW_MINIMIZE
	 */
	[Event(name="windowminimize", type="com.wps.shell.events.WindowEvent")]

	/**
	 * 窗口还原事件。
	 * @eventType com.wps.shell.events.WindowEvent.WINDOW_RESTORE
	 */
	[Event(name="windowrestore", type="com.wps.shell.events.WindowEvent")]

	/**
	 * 窗口关闭事件。
	 * @eventType com.wps.shell.events.WindowEvent.WINDOW_CLOSE
	 */
	[Event(name="windowclose", type="com.wps.shell.events.WindowEvent")]

	[Style(name="horizontalResizerCursor", type="Class", inherit="no")]
	[Style(name="verticalResizerCursor", type="Class", inherit="no")]
	[Style(name="diagonalResizerCursor", type="Class", inherit="no")]
	[Style(name="backdiagonalResizerCursor", type="Class", inherit="no")]

	[Style(name="closeButtonSkin", type="Class", inherit="no")]
	[Style(name="maximizeButtonSkin", type="Class", inherit="no")]
	[Style(name="minimizeButtonSkin", type="Class", inherit="no")]
	[Style(name="restoreButtonSkin", type="Class", inherit="no")]

	[Style(name="borderThicknessBottom", type="Number", format="Length")]
	[Style(name="borderThicknessLeft", type="Number", format="Length")]
	[Style(name="borderThicknessRight", type="Number", format="Length")]
	[Style(name="borderThicknessTop", type="Number", format="Length")]

	[Style(name="maximizedBackGroundColor", type="Number", format="color")]

	/**
	 * 仿Windows窗口控件
	 * @author zhuohongtao
	 */
	public class CommonWindow extends SkinnableContainer 
	{
		//--------------------------------------------------------------------------
		//
		//  公开常量
		//
		//--------------------------------------------------------------------------

		/**
		 * 窗口状态常量，常规状态。
		 */
		public static const WINDOW_STATE_NORMAL: int = 0;

		/**
		 * 窗口状态常量，最大化。
		 */
		public static const WINDOW_STATE_MAXIMIZED: int = 1;

		/**
		 * 窗口状态常量，最小化。
		 */
		public static const WINDOW_STATE_MINIMIZED: int = 2;

		//--------------------------------------------------------------------------
		//
		//  私有常量
		//
		//--------------------------------------------------------------------------

		/**
		 * 点击测试常量
		 * @private
		 */
		private static const HITTEST_NOWHERE: int		= -1;
		private static const HITTEST_CLIENT: int		= 0;
		private static const HITTEST_LEFT: int			= 1;
		private static const HITTEST_RIGHT: int			= 2;
		private static const HITTEST_TOP: int			= 4;
		private static const HITTEST_BOTTOM: int		= 8;
		private static const HITTEST_TOPLEFT: int		= HITTEST_TOP | HITTEST_LEFT;
		private static const HITTEST_TOPRIGHT: int		= HITTEST_TOP | HITTEST_RIGHT;
		private static const HITTEST_BOTTOMLEFT: int	= HITTEST_BOTTOM | HITTEST_LEFT;
		private static const HITTEST_BOTTOMRIGHT: int	= HITTEST_BOTTOM | HITTEST_RIGHT;
		private static const HITTEST_CAPTION: int		= 0x10;
		private static const HITTEST_ICON: int			= 0x20;
		private static const HITTEST_MINBUTTON: int		= 0x30;
		private static const HITTEST_MAXBUTTON: int		= 0x40;
		private static const HITTEST_CLOSEBUTTON: int	= 0x50;

		/**
		 * 宽高限定值。
		 * @private
		 */
		protected static const MIN_WIDTH: int			= 120;
		protected static const MIN_HEIGHT: int			= 26;
		protected static const MINEDWIDTH: int			= 180;
		protected static const BORDERWIDTH: int			= 4;

		/**
		 * 更改窗口大小时，记录窗口的原始信息。
		 * @private
		 */
		private static var _lastX: Number;
		private static var _lastY: Number;
		private static var _oldWidth: Number;
		private static var _oldHeight: Number;
		private static var _oldRight: Number;
		private static var _oldBottom: Number;

		//--------------------------------------------------------------------------
		//
		//  私有变量
		//
		//--------------------------------------------------------------------------

		/**
		 * 标题栏按钮样式
		 */
		private var minimizeButtonSkin: Class;
		private var maximizeButtonSkin: Class;
		private var _restoreButtonSkin: Class;
		private var closeButtonSkin: Class;

		private var contentBackGroundColor: Number;
		private var maximizedBackgroundColor: Number;

		private var _normalRect: Rectangle;
		private var _windowState: int = WINDOW_STATE_NORMAL;
		private var _hittestCode: int = HITTEST_CLIENT;
		private var _cursorID: int = CursorManager.NO_CURSOR;

		/**
		 * 窗口属性相关
		 */
		private var _minimizable: Boolean = true;
		private var _maximizable: Boolean = true;
		private var _closeable: Boolean = true;
		private var _resizeable: Boolean = true;
		private var _mdiChildWindow: Boolean = false;

		/**
		 * 标题
		 */
		private var _title: String;

		//--------------------------------------------------------------------------
		//
		//  组件
		//
		//--------------------------------------------------------------------------

		/**
		 * 背景边框
		 */
		public var backGround: ComWindowBackGround;

		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------

		[ Embed (source="../../../../../assets/cusor.png")]
		[ Bindable ]
		public var imgCls:Class;
		/**
		 * 构造函数。
		 */
		public function CommonWindow()
		{
			super();
			setStyle("horizontalResizerCursor", imgCls);
			setStyle("verticalResizerCursor", imgCls);
			setStyle("diagonalResizerCursor", imgCls);
			setStyle("backdiagonalResizerCursor", imgCls);
			setStyle("horizontalResizerCursor", imgCls);
			
			creationPolicy = ContainerCreationPolicy.ALL;
			autoLayout = true;
			isPopUp = true;
			minWidth = MIN_WIDTH;
			minHeight = MIN_HEIGHT;

			addEventListener(MouseEvent.MOUSE_DOWN, _mouseDownHandler);
			addEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
			addEventListener(MouseEvent.MOUSE_OUT, _mouseOutHandler);
			addEventListener(MoveEvent.MOVE, _moveHandler);
			addEventListener(ResizeEvent.RESIZE, _resizeHandler);

			_normalRect = new Rectangle(x, y, width, height);

		}

		//--------------------------------------------------------------------------
		//
		//  ILifecycle
		//
		//--------------------------------------------------------------------------

		/**
		 * @private
		 */
		public function destroy(): void
		{
			removeEventListener(MouseEvent.MOUSE_DOWN, _mouseDownHandler);
			removeEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
			removeEventListener(MouseEvent.MOUSE_OUT, _mouseOutHandler);
			removeEventListener(MoveEvent.MOVE, _moveHandler);
			removeEventListener(ResizeEvent.RESIZE, _resizeHandler);
			if(backGround)
			{
				backGround.removeEventListener(MouseEvent.DOUBLE_CLICK, _titleDblClickHandler);
				backGround.destroy();
				var skinGroup: Group = Group(this.skin);
				skinGroup.removeElementAt(0);
				//				skinGroup.removeAllElements();
				backGround = null;
			}
		}

		//--------------------------------------------------------------------------
		//
		//  公开属性
		//
		//--------------------------------------------------------------------------

		/**
		 * 是否允许窗口最小化。
		 */
		[Inspectable(category="General", default="true")]
		public function get minimizable(): Boolean
		{
			return _minimizable;
		}

		public function set minimizable(value: Boolean): void
		{
			if (_minimizable != value)
			{
				_minimizable = value;
				if (backGround && backGround.minimizeButton)
					backGround.minimizeButton.visible = value;
			}
		}

		/**
		 * 是否允许窗口最大化。
		 */
		[Inspectable(category="General", default="true")]
		public function get maximizable(): Boolean
		{
			return _maximizable;
		}

		public function set maximizable(value: Boolean): void
		{
			if (_maximizable != value)
			{
				_maximizable = value;
				if (backGround && backGround.maximizeButton)
					backGround.maximizeButton.visible = value;
			}
		}

		/**
		 * 是否允许窗口关闭。
		 */
		[Inspectable(category="General", default="true")]
		public function get closeable(): Boolean
		{
			return _closeable;
		}

		public function set closeable(value: Boolean): void
		{
			if (_closeable != value)
			{
				_closeable = value;
				if (backGround && backGround.closeButton)
					backGround.closeButton.visible = value;
			}
		}

		/**
		 * 是否允许窗口更改大小。
		 */
		[Inspectable(category="General", default="true")]
		public function get resizeable(): Boolean
		{
			return _resizeable;
		}

		public function set resizeable(value: Boolean): void
		{
			if (_resizeable != value)
			{
				_resizeable = value;
				if (_resizeable)
				{
					addEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
					addEventListener(MouseEvent.MOUSE_OUT, _mouseOutHandler);
				}
				else
				{
					removeEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
					removeEventListener(MouseEvent.MOUSE_OUT, _mouseOutHandler);
				}
			}
		}

		/**
		 * 是否为MDI子窗口。
		 */
		[Inspectable(category="General", default="false")]
		public function get mdiChildWindow(): Boolean
		{
			return _mdiChildWindow;
		}

		public function set mdiChildWindow(value: Boolean): void
		{
			if (_mdiChildWindow != value)
			{
				_mdiChildWindow = value;
				invalidateProperties();
			}
		}

		/**
		 * 窗口标题。
		 */
		[Bindable]
		public function get caption(): String
		{
			return _title;
		}

		public function set caption(value: String): void
		{
			_title = value;
			if(backGround)
				backGround.text = value;
			dispatchEvent(new Event('titleChanged'));
		}

		[Bindable]
		public function get title(): String
		{
			return caption;
		}

		public function set title(v: String): void
		{
			caption = v;
		}

		/**
		 * 窗口状态。
		 */
		public function get windowState(): int
		{
			return _windowState;
		}

		public function set windowState(value: int): void
		{
			if (_windowState != value)
			{
				switch (value)
				{
					case WINDOW_STATE_NORMAL:
						restore();
						break;
					case WINDOW_STATE_MAXIMIZED:
						maximize();
						break;
					case WINDOW_STATE_MINIMIZED:
						minimize();
						break;
				}
			}
		}

		//--------------------------------------------------------------------------
		//
		//  公开方法
		//
		//--------------------------------------------------------------------------

		/**
		 * 激活窗口。
		 */
		public function activate(): void
		{
			var focus: DisplayObject = systemManager.stage.focus;
			if ((focus == null) || (focus != this && !contains(focus)))
				setFocus();

			if (_mdiChildWindow && parent)
				parent.setChildIndex(this, parent.numChildren - 1);

			dispatchEvent(new WindowEvent(WindowEvent.WINDOW_ACTIVATE));
		}

		/**
		 * 最大化窗口。
		 */
		public function maximize(): void
		{
			if (_windowState != WINDOW_STATE_MAXIMIZED)
			{
				_maxStyle();
				x = 0;
				y = 0;
				percentWidth = 100;
				percentHeight = 100;

				if (backGround && backGround.maximizeButton)
				{
					backGround.maximizeButton.setStyle("skinClass", _restoreButtonSkin);
					backGround.maximizeButton.toolTip = _restoreTooltip;
				}
				if (backGround.minimizeButton)
				{
					backGround.minimizeButton.setStyle("skinClass", minimizeButtonSkin);
					backGround.minimizeButton.toolTip = _minimizeTooltip;
				}

				dispatchEvent(new WindowEvent(WindowEvent.WINDOW_MAXIMIZE));
			}
		}

		/**
		 * 最小化窗口。
		 */
		public function minimize(): void
		{
			if (_windowState != WINDOW_STATE_MINIMIZED)
			{
				_minStyle();
				//----------------------------------------------------------------------------------------------------
				height = contentGroup.y;
				width = MINEDWIDTH;

				if (backGround && backGround.maximizeButton)
				{
					backGround.maximizeButton.setStyle("skinClass", maximizeButtonSkin);
					backGround.maximizeButton.toolTip = _maximizeTooltip;
				}
				if (backGround && backGround.minimizeButton)
				{
					backGround.minimizeButton.setStyle("skinClass", _restoreButtonSkin);
					backGround.minimizeButton.toolTip = _restoreTooltip;
				}

				dispatchEvent(new WindowEvent(WindowEvent.WINDOW_MINIMIZE));
			}
		}

		/**
		 * 还原窗口为正常大小。
		 */
		public function restore(): void
		{
			_restoreTo(_normalRect.x, _normalRect.y);
		}

		/**
		 * 关闭窗口。
		 */
		public function closeWindow(): void
		{
			dispatchEvent(new WindowEvent(WindowEvent.WINDOW_CLOSE));
		}

		//--------------------------------------------------------------------------
		//
		//  重载方法
		//
		//--------------------------------------------------------------------------

		/**
		 * @private
		 * 语言环境发生变化时，更新标题栏按钮的tooltip。
		 */
		override protected function resourcesChanged():void
		{
			super.resourcesChanged();

			if (backGround && backGround.minimizeButton)
				backGround.minimizeButton.toolTip = _windowState == WINDOW_STATE_MINIMIZED ? _restoreTooltip : _minimizeTooltip;
			if (backGround && backGround.maximizeButton)
				backGround.maximizeButton.toolTip = _windowState == WINDOW_STATE_MAXIMIZED ? _restoreTooltip : _maximizeTooltip;
			if (backGround && backGround.closeButton)
				backGround.closeButton.toolTip = _closeTooltip;
		}

		override protected function createChildren():void
		{
			super.createChildren();

			contentBackGroundColor = getStyle("contentBackgroundColor");
			maximizedBackgroundColor = getStyle("maximizedBackGroundColor");
			closeButtonSkin = getStyle("closeButtonSkin");
			maximizeButtonSkin = getStyle("maximizeButtonSkin");
			minimizeButtonSkin = getStyle("minimizeButtonSkin");
			_restoreButtonSkin  = getStyle("restoreButtonSkin");
			//初始化背景边框
			var skinGroup: Group = Group(this.skin);
			backGround = new ComWindowBackGround();
			backGround.addEventListener(MouseEvent.DOUBLE_CLICK, _titleDblClickHandler);
			backGround.text = title;
			backGround.doubleClickEnabled = true;
			_setBorderInfo("headerHeight");
			_setBorderInfo("borderThicknessRight");
			_setBorderInfo("borderThicknessLeft");
			_setBorderInfo("borderThicknessBottom");
			if(!isNaN(contentBackGroundColor))
				backGround.contentBackGroundColor = contentBackGroundColor;
			skinGroup.addElementAt(backGround, 0);

			//初始化按钮事件
			_initButton(backGround.closeButton, closeButtonSkin, true, _closeTooltip, _closeButtonClickHandler);
			_initButton(backGround.minimizeButton, minimizeButtonSkin, true, _minimizeTooltip, _minButtonClickHandler);
			_initButton(backGround.maximizeButton, maximizeButtonSkin, true, _maximizeTooltip, _maxButtonClickHandler);

			//当调整window的大小到足够小时，contentGroup不应显示出来
			contentGroup.clipAndEnableScrolling = true;

			invalidateDisplayList();
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			if(backGround)
			{
				contentGroup.left = backGround.borderThicknessLeft;
				contentGroup.right = backGround.borderThicknessRight;
				contentGroup.bottom = backGround.borderThicknessBottom;
				contentGroup.top = backGround.titleHeight;
			}
		}

		override protected function commitProperties():void
		{
			super.commitProperties();
			if(backGround)
			{
				backGround.closeButton.visible = closeable;
				backGround.minimizeButton.visible = minimizable;
				backGround.maximizeButton.visible = maximizable;
			}
		}

		/**
		 * @private
		 * 样式发生改变时，更新标题栏按钮样式
		 */
		override public function styleChanged(styleProp: String): void
		{
			super.styleChanged(styleProp);

			var allStyles:Boolean = !styleProp || styleProp == "styleName";
			if (allStyles || styleProp == "closeButtonSkin")
			{
				closeButtonSkin = getStyle("closeButtonSkin");
				if (closeButtonSkin && backGround  && backGround.closeButton)
					backGround.closeButton.setStyle("skinClass", closeButtonSkin);
			}

			if (allStyles || styleProp == "restoreButtonSkin")
			{
				_restoreButtonSkin = getStyle("restoreButtonSkin");
			}

			if (allStyles || styleProp == "maximizeButtonSkin")
			{
				maximizeButtonSkin = getStyle("maximizeButtonSkin");
				if (backGround && backGround  && backGround.maximizeButton)
				{
					var maxskin: Class = (_windowState == WINDOW_STATE_MAXIMIZED) ? _restoreButtonSkin : maximizeButtonSkin;
					if (maxskin)
						backGround.maximizeButton.setStyle("skinClass", maxskin);
				}
			}

			if (allStyles || styleProp == "minimizeButtonSkin")
			{
				minimizeButtonSkin = getStyle("minimizeButtonSkin");
				if (backGround  && backGround.minimizeButton)
				{
					var minskin: Class = (_windowState == WINDOW_STATE_MINIMIZED) ? _restoreButtonSkin : minimizeButtonSkin;
					if (minskin)
						backGround.minimizeButton.setStyle("skinClass", minskin);
				}
			}

			if(allStyles || styleProp == "headerHeight")
				_setBorderInfo("headerHeight");

			if(allStyles || styleProp == "borderThicknessTop")
				_setBorderInfo("borderThicknessTop");

			if(allStyles || styleProp == "borderThicknessRight")
				_setBorderInfo("borderThicknessRight");

			if(allStyles || styleProp == "borderThicknessLeft")
				_setBorderInfo("borderThicknessLeft");

			if(allStyles || styleProp == "borderThicknessBottom")
				_setBorderInfo("borderThicknessBottom");
		}

		//--------------------------------------------------------------------------
		//
		//  限制窗口的范围
		//
		//--------------------------------------------------------------------------

		/**
		 * @private
		 * 限制窗口的范围。
		 */
		override public function move(x: Number, y: Number): void
		{
			super.move(int(Math.max(x, minX)), int(Math.max(y, minY)));
		}

		/**
		 * @private
		 * 限制窗口的范围。
		 */
		override public function set x(value: Number): void
		{
			super.x = int(Math.max(value, minX));
		}

		/**
		 * @private
		 * 限制窗口的范围。
		 */
		override public function set y(value: Number): void
		{
			super.y = int(Math.max(value, minY));
		}

		/**
		 * @private
		 * 限制窗口的范围。
		 */
		override public function set minHeight(value: Number): void
		{
			super.minHeight = int(Math.max(value, MIN_HEIGHT));
		}
		/**
		 * @private
		 * 限制窗口的范围。
		 */
		override public function set minWidth(value: Number): void
		{
			super.minWidth = int(Math.max(value, MIN_WIDTH));
		}

		/**
		 * 窗口位置的极限值，最左侧的位置。
		 */
		protected function get minX(): Number
		{
			return 0;
		}

		/**
		 * 窗口位置的极限值，最上方的位置。
		 */
		protected function get minY(): Number
		{
			return 0;
		}

		protected function get maxX(): Number
		{
			return (parent.width - width);
		}

		protected function get maxY(): Number
		{
			return (parent.height - height);
		}

		//--------------------------------------------------------------------------
		//
		//  限制窗口的范围
		//
		//--------------------------------------------------------------------------

		/**
		 * 启动更改窗口大小的操作。
		 */
		protected function startResizing(event: MouseEvent): void
		{
			_lastX = event.stageX - x;
			_lastY = event.stageY - y;
			_oldWidth = width;
			_oldHeight = height;
			_oldRight = x + width;
			_oldBottom = y + height;

			systemManager.addEventListener(MouseEvent.MOUSE_MOVE, _systemMouseMoveHandler, true);
			systemManager.addEventListener(MouseEvent.MOUSE_UP, _systemMouseUpHandler, true);
			systemManager.stage.addEventListener(Event.MOUSE_LEAVE, _systemMouseLeaveHandler);
			removeEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
			removeEventListener(MouseEvent.MOUSE_OUT, _mouseOutHandler);
		}

		/**
		 * 结束更改窗口大小的操作。
		 */
		protected function stopResizing(): void
		{
			systemManager.removeEventListener(MouseEvent.MOUSE_MOVE, _systemMouseMoveHandler, true);
			systemManager.removeEventListener(MouseEvent.MOUSE_UP, _systemMouseUpHandler, true);
			systemManager.stage.removeEventListener(Event.MOUSE_LEAVE, _systemMouseLeaveHandler);
			addEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
			addEventListener(MouseEvent.MOUSE_OUT, _mouseOutHandler);
		}

		//--------------------------------------------------------------------------
		//
		//  事件处理器
		//
		//--------------------------------------------------------------------------

		/**
		 * 最大化按钮鼠标点击事件处理器。
		 */
		private function _maxButtonClickHandler(event: MouseEvent): void
		{
			if (event.target == backGround.maximizeButton)
			{
				if (_windowState != WINDOW_STATE_MAXIMIZED)
					maximize();
				else
					restore();
			}
		}

		/**
		 * 最小化按钮鼠标点击事件处理器。
		 */
		private function _minButtonClickHandler(event: MouseEvent): void
		{
			if (event.target == backGround.minimizeButton)
			{
				if (_windowState != WINDOW_STATE_MINIMIZED)
					minimize();
				else
					restore();
			}
		}

		/**
		 * 关闭按钮鼠标点击事件处理器。
		 */
		private function _closeButtonClickHandler(event: MouseEvent): void
		{
			dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
		}

		/**
		 * 标题栏双击，将窗口最大化。
		 */
		private function _titleDblClickHandler(event: MouseEvent): void
		{
			if (event.target == backGround.minimizeButton || event.target == backGround.maximizeButton || event.target == backGround.closeButton)
				return;
			if (_resizeable)
				_windowState != WINDOW_STATE_MAXIMIZED ? maximize() : restore();
		}

		/**
		 * 鼠标按钮下时，激活自己。
		 */
		private function _mouseDownHandler(event: MouseEvent): void
		{
			activate();
			if( (event.target==backGround.closeButton) || (event.target==backGround.minimizeButton) || (event.target==backGround.maximizeButton) )
				return;
			//先处理拖拽
			if(_getHitCode(event) == HITTEST_CAPTION)
				_startDragLater();
			else
				if (_resizeable && (_cursorID != CursorManager.NO_CURSOR))
					startResizing(event);
		}

		/**
		 * 鼠标移动的事件处理器，点击测试
		 */
		private function _mouseMoveHandler(event: MouseEvent): void
		{
			var hitcode: int = _getHitCode(event);
			if (hitcode != _hittestCode)
			{
				_hittestCode = hitcode;
				if (_cursorID != CursorManager.NO_CURSOR)
					cursorManager.removeCursor(_cursorID);

				var style: String = "";
				if (hitcode == HITTEST_LEFT || hitcode == HITTEST_RIGHT)
					style = "horizontalResizerCursor";
				else if (hitcode == HITTEST_TOP || hitcode == HITTEST_BOTTOM)
					style = "verticalResizerCursor";
				else if (hitcode == HITTEST_TOPLEFT || hitcode == HITTEST_BOTTOMRIGHT)
					style = "diagonalResizerCursor";
				else if (hitcode == HITTEST_TOPRIGHT || hitcode == HITTEST_BOTTOMLEFT)
					style = "backdiagonalResizerCursor";

				if (style != "")
					_cursorID = cursorManager.setCursor(getStyle(style) as Class, CursorManagerPriority.HIGH);
				else
					_cursorID = CursorManager.NO_CURSOR;
					
			}
		}

		/**
		 * 鼠标移出窗口的事件处理器。
		 */
		private function _mouseOutHandler(event: MouseEvent): void
		{
			_hittestCode = HITTEST_NOWHERE;
			if (_cursorID != CursorManager.NO_CURSOR)
			{
				cursorManager.removeCursor(_cursorID);
				_cursorID = CursorManager.NO_CURSOR;
			}
		}

		/**
		 * 窗口移动的事件处理器，记录窗口的最新位置。
		 */
		private function _moveHandler(event: MoveEvent): void
		{
			if (_windowState != WINDOW_STATE_MAXIMIZED)
			{
				_normalRect.x = x;
				_normalRect.y = y;
			}
		}

		/**
		 * 更改窗口大小的事件处理器，记录窗口的最新大小。
		 */
		private function _resizeHandler(event: ResizeEvent): void
		{
			if (_windowState == WINDOW_STATE_NORMAL)
			{
				_normalRect.width = width;
				_normalRect.height = height;
			}
		}

		/**
		 * 启动更改窗口大小的操作后，更改窗口大小。
		 */
		private function _systemMouseMoveHandler(event: MouseEvent): void
		{
			event.stopImmediatePropagation();

			var curX: Number = event.stageX - _lastX;
			var curY: Number = event.stageY - _lastY;

			var newX: Number = x;
			var newY: Number = y;
			var newWidth: Number = width;
			var newHeight: Number = height;

			if ((_hittestCode & HITTEST_LEFT) == HITTEST_LEFT)
			{
				newX = Math.max(curX, -width + BORDERWIDTH);
				newWidth = _oldRight - newX;
				if (newWidth < minWidth)
				{
					newX -= minWidth - newWidth;
					newWidth = minWidth;
				}
			}
			else if ((_hittestCode & HITTEST_RIGHT) == HITTEST_RIGHT)
			{
				newWidth = Math.max(_oldWidth + curX - x, minWidth);
			}

			if ((_hittestCode & HITTEST_TOP) == HITTEST_TOP)
			{
				newY = Math.max(curY, (-contentGroup.y + BORDERWIDTH));
				newHeight = _oldBottom - newY;
				if (newHeight < minHeight)
				{
					newY -= minHeight - newHeight;
					newHeight = minHeight;
				}
			}
			else if ((_hittestCode & HITTEST_BOTTOM) == HITTEST_BOTTOM)
			{
				newHeight = Math.max(_oldHeight + curY - y, minHeight);
			}

			x = newX;
			y = newY;
			width = newWidth;
			height = newHeight;
		}

		/**
		 * 停止更改窗口大小。
		 */
		private function _systemMouseUpHandler(event: MouseEvent): void
		{
			stopResizing();
		}

		/**
		 * 停止更改窗口大小。
		 */
		private function _systemMouseLeaveHandler(event: MouseEvent): void
		{
			stopResizing();
		}

		/**
		 * 取消拖放的事件处理器
		 */
		private function _cancelDragHandler(event: MouseEvent): void
		{
			_completeStartDrag();
		}

		//--------------------------------------------------------------------------
		//
		//  辅助方法
		//
		//--------------------------------------------------------------------------

		/**
		 *  初始化按钮
		 */
		private function _initButton(button: spark.components.Button, btnSkinClass: Class, visible: Boolean, toolTip: String, listener: Function): void
		{
			if (btnSkinClass)
				button.setStyle("skinClass", btnSkinClass);
			button.toolTip = toolTip;
			button.explicitWidth = button.explicitHeight = 16;

			button.focusEnabled = false;
			button.visible = visible;
			button.enabled = visible;
			button.addEventListener(MouseEvent.CLICK, listener);
		}

		/**
		 *   获取点击测试码
		 */
		private function _getHitCode(event: MouseEvent) : int
		{
			var hitcode: int = HITTEST_CLIENT;
			if (_windowState == WINDOW_STATE_NORMAL
				&& (event.target == this.skin || event.target == backGround.skin))
			{
				if (event.localX < BORDERWIDTH)
					hitcode |= HITTEST_LEFT;
				else if (event.localX > width - BORDERWIDTH)
					hitcode |= HITTEST_RIGHT;

				if (event.localY < BORDERWIDTH)
					hitcode |= HITTEST_TOP;
				else if (event.localY < contentGroup.y)
				{
					if (hitcode == HITTEST_CLIENT)
						hitcode = HITTEST_CAPTION;
				}
				else if (event.localY > height - BORDERWIDTH)
					hitcode |= HITTEST_BOTTOM;
			}
			else if (_windowState == WINDOW_STATE_MAXIMIZED)
			{
				hitcode = HITTEST_CLIENT;
			}
			else if (_windowState == WINDOW_STATE_MINIMIZED)
			{
				hitcode = HITTEST_CAPTION;
			}
			return hitcode;
		}

		/**
		 *   根据样式设定边框
		 */
		private function _setBorderInfo(value: String): void
		{
			if(value == "headerHeight")
			{
				var headerHeight: Number = getStyle("headerHeight");
				if(isNaN(headerHeight))
					headerHeight = MIN_HEIGHT;
				if(backGround)
					backGround.titleHeight = headerHeight;
				if(contentGroup)
					contentGroup.top = headerHeight;
			}

			if(value == "borderThicknessTop")
			{
				//暂不做处理，标题高度取决于headerHeight
			}

			if(value == "borderThicknessRight")
			{
				var borderThicknessRight: Number = getStyle("borderThicknessRight");
				if(isNaN(borderThicknessRight))
					borderThicknessRight = BORDERWIDTH;
				if(backGround)
					backGround.borderThicknessRight = borderThicknessRight;
				if(contentGroup)
					contentGroup.right = borderThicknessRight;
			}

			if(value == "borderThicknessLeft")
			{
				var borderThicknessLeft: Number = getStyle("borderThicknessLeft");
				if(isNaN(borderThicknessLeft))
					borderThicknessLeft = BORDERWIDTH;
				if(backGround)
					backGround.borderThicknessLeft = borderThicknessLeft;
				if(contentGroup)
					contentGroup.left = borderThicknessLeft;
			}

			if(value == "borderThicknessBottom")
			{
				var borderThicknessBottom: Number = getStyle("borderThicknessBottom");
				if(isNaN(borderThicknessBottom))
					borderThicknessBottom = BORDERWIDTH;
				if(backGround)
					backGround.borderThicknessBottom = borderThicknessBottom;
				if(contentGroup)
					contentGroup.bottom = borderThicknessBottom;
			}
		}

		/**
		 * 当鼠标开始拖动时才启动拖放
		 */
		private function _startDragLater(): void
		{
			startDrag();
			systemManager.addEventListener(MouseEvent.MOUSE_UP, _cancelDragHandler, true);
			systemManager.stage.addEventListener(Event.MOUSE_LEAVE, _cancelDragHandler);
			removeEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
			removeEventListener(MouseEvent.MOUSE_OUT, _mouseOutHandler);
		}

		/**
		 * 完成拖放的启动
		 */
		private function _completeStartDrag(): void
		{
			stopDrag();

			//拖拽结束后，调整位置
			this.x = Math.min(this.x, maxX);
			this.x = Math.max(this.x, minX);
			this.y = Math.min(this.y, maxY);
			this.y = Math.max(this.y , minY);

			systemManager.removeEventListener(MouseEvent.MOUSE_UP, _cancelDragHandler, true);
			systemManager.stage.removeEventListener(Event.MOUSE_LEAVE, _cancelDragHandler);
			addEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
			addEventListener(MouseEvent.MOUSE_OUT, _mouseOutHandler);
		}

		/**
		 * 还原窗口为正常大小，并且移动窗口到制定的位置。
		 * @param x 窗口的x坐标
		 * @param y 窗口的y坐标
		 */
		private function _restoreTo(x: int, y: int): void
		{
			if (_windowState != WINDOW_STATE_NORMAL)
			{
				_normalStyle();
				move(x, y);
				width = _normalRect.width;
				height = _normalRect.height;

				if (backGround && backGround.maximizeButton)
				{
					backGround.maximizeButton.setStyle("skinClass", maximizeButtonSkin);
					backGround.maximizeButton.toolTip = _maximizeTooltip;
				}
				if (backGround && backGround.minimizeButton)
				{
					backGround.minimizeButton.setStyle("skinClass", minimizeButtonSkin);
					backGround.minimizeButton.toolTip = _minimizeTooltip;
				}

				dispatchEvent(new WindowEvent(WindowEvent.WINDOW_RESTORE));
			}
		}

		/**
		 * 保存窗口的大小和位置。
		 */
		private function _saveState(): void
		{
			if (_windowState == WINDOW_STATE_NORMAL)
			{
				_normalRect.x = x;
				_normalRect.y = y;
				_normalRect.width = width;
				_normalRect.height = height;
			}
		}

		/**
		 * 窗口最大化时所使用的样式。
		 */
		private function _maxStyle(): void
		{
			_saveState();
			_windowState = WINDOW_STATE_MAXIMIZED;
			if (_mdiChildWindow)
			{
				setStyle("headerHeight", 0);
				setStyle("borderStyle", "none");
				setStyle("borderThicknessTop", 0);
				setStyle("borderThicknessRight", 0);
				setStyle("borderThicknessLeft", 0);
				setStyle("borderThicknessBottom", 0);
				if(!isNaN(maximizedBackgroundColor))
				{
					if(backGround)
						backGround.contentBackGroundColor = maximizedBackgroundColor;
				}
			}
		}

		/**
		 * 恢复窗口的默认样式。
		 */
		private function _defaultStyle(): void
		{
			clearStyle("borderStyle");
			clearStyle("cornerRadius");
			clearStyle("headerHeight");
			clearStyle("borderThicknessTop");
			clearStyle("borderThicknessRight");
			clearStyle("borderThicknessLeft");
			clearStyle("borderThicknessBottom");

			if(!isNaN(contentBackGroundColor))
			{
				if(backGround)
					backGround.contentBackGroundColor = contentBackGroundColor;
			}
		}

		/**
		 * 窗口正常大小时所使用的样式。
		 */
		private function _normalStyle(): void
		{
			_windowState = WINDOW_STATE_NORMAL;
			if (_mdiChildWindow)
				_defaultStyle();
		}

		/**
		 * 窗口最小化时所使用的样式。
		 */
		private function _minStyle(): void
		{
			_saveState();
			_windowState = WINDOW_STATE_MINIMIZED;
			if (_mdiChildWindow)
				_defaultStyle();
		}

		/**
		 * 关闭按钮的工具提示。
		 */
		private function get _closeTooltip(): String
		{
			return resourceManager.getString("wpscontrols", "close");
		}

		/**
		 * 最小化按钮的工具提示。
		 */
		private function get _minimizeTooltip(): String
		{
			return resourceManager.getString("wpscontrols", "minimize");
		}

		/**
		 * 最大化按钮的工具提示。
		 */
		private function get _maximizeTooltip(): String
		{
			return resourceManager.getString("wpscontrols", "maximize");
		}

		/**
		 * 还原按钮的工具提示。
		 */
		private function get _restoreTooltip(): String
		{
			return resourceManager.getString("wpscontrols", "restore");
		}
	}
}
